package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.AdvSpaceQueryDTO;
import com.jeff.tianti.cms.entity.AdvSpace;
import com.jeff.tianti.cms.entity.Article;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ArticleDaoImpl类
 * @date 2017-03-16
 */

public class AdvSpaceDaoImpl extends CustomBaseSqlDaoImpl implements AdvSpaceCustom {

    public PageModel<AdvSpace> queryAdvSpacePage(AdvSpaceQueryDTO advSpaceQueryDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select a from AdvSpace a where 1=1 ");
        if (advSpaceQueryDTO != null) {
            if (StringUtils.isNotBlank(advSpaceQueryDTO.getDicCode())) {
                hql.append(" and a.dicCode = :dicCode ");
                map.put("dicCode", advSpaceQueryDTO.getDicCode());
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, advSpaceQueryDTO.getCurrentPage(), advSpaceQueryDTO.getPageSize());
    }

    @Override
    public int updateSelectiveById(AdvSpace advSpace) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update AdvSpace t set");
        if (advSpace != null) {
            if (!StringUtils.isNotBlank(advSpace.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(advSpace.getUrl())) {
                hql.append(" t.url = :url ");
                map.put("url", advSpace.getUrl());
            }

            if (StringUtils.isNotBlank(advSpace.getTitle())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.title = :title ");
                } else {
                    hql.append(" t.title = :title ");
                }
                map.put("title", advSpace.getTitle());
            }
            if (StringUtils.isNotBlank(advSpace.getContent())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.content = :content ");
                } else {
                    hql.append(" t.content = :content ");
                }
                map.put("content", advSpace.getContent());
            }

            if (StringUtils.isNotBlank(advSpace.getDicCode())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.dicCode = :dicCode ");
                } else {
                    hql.append(" t.dicCode = :dicCode ");
                }
                map.put("dicCode", advSpace.getDicCode());
            }

            if (StringUtils.isNotBlank(advSpace.getAdvUrl())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.advUrl = :advUrl ");
                } else {
                    hql.append(" t.advUrl = :advUrl ");
                }
                map.put("advUrl", advSpace.getAdvUrl());
            }

            if (StringUtils.isNotBlank(advSpace.getDeleteFlag())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.deleteFlag = :deleteFlag ");
                } else {
                    hql.append(" t.deleteFlag = :deleteFlag ");
                }
                map.put("deleteFlag", advSpace.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(advSpace.getChannel())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.channel = :channel ");
                } else {
                    hql.append(" t.channel = :channel ");
                }
                map.put("channel", advSpace.getChannel());
            }
            if (StringUtils.isNotBlank(advSpace.getOrderNo())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.orderNo = :orderNo ");
                } else {
                    hql.append(" t.orderNo = :orderNo ");
                }
                map.put("orderNo", advSpace.getOrderNo());
            }
            hql.append("where t.id=:id ");
            map.put("id", advSpace.getId());
        }
        return this.update(hql.toString(), map);
    }
}
